package com.application;

import com.imessage.comm.Constants;
import com.imessage.service.InitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Component
@Order(value = 1)
public class MainApplicationRunner implements ApplicationRunner{

    @Autowired
    private InitService initService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("初始化数据--------------");
        try {
            initService.initMenu();
        } catch (Exception e) {
            log.error("", e);
        }
        try {
            initService.initUser();
        } catch (Exception e) {
            log.error("", e);
        }
        try {
            initService.initConfig();
        } catch (Exception e) {
            log.error("", e);
        }
        try {
            initService.initGroup();
        } catch (Exception e) {
            log.error("", e);
        }

    }

    private void getIP_PORT_PATH() {
        try {
            while (StringUtils.isBlank(Constants.IP_PORT_PATH)) {
                {
                    BufferedReader in = null;
                    try {
                        URL realUrl = new URL("http://ip.chinaz.com");
                        // 打开和URL之间的连接
                        URLConnection connection = realUrl.openConnection();
                        // 设置通用的请求属性
                        connection.setRequestProperty("accept", "*/*");
                        connection.setRequestProperty("connection", "Keep-Alive");
                        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                        connection.setConnectTimeout(10000);
                        connection.setReadTimeout(10000);
                        // 建立实际的连接
                        connection.connect();
                        // 定义 BufferedReader输入流来读取URL的响应
                        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                        StringBuffer sb = new StringBuffer();
                        String line;
                        while ((line = in.readLine()) != null) {
                            sb.append(line);
                        }
                        Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
                        Matcher m = p.matcher(sb.toString());
                        if (m.find()) {
                            Constants.IP_PORT_PATH = m.group(1);
                            log.info("IP_PORT_PATH获取成功:" + Constants.IP_PORT_PATH);
                        }
                    } catch (Exception e) {
                        log.error("IP_PORT_PATH获取失败,等待再次获取。");
                    } finally {
                        try {
                            if (in != null) {
                                in.close();
                            }
                        } catch (Exception e2) {
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }
    }

}
